$(document).ready(init);

function init() {
	$("#left").accordion({animate:false});
	
	$('#left').accordion('select', '我的预约仪器');
	
	showContent({selfOrder : 'selfOrder'});
	
	$('#search').click(doSearch);
}

function doSearch() {
	showContent({
		startTime : $.trim($('#startTime').val()),
		endTime : $.trim($('#endTime').val()),
		selfOrder : 'selfOrder'
	});
	
}

/**
 * 显示信息
 */
function showContent(queryParams) {
	$('#appointments').datagrid({
	    title:'',
	    singleSelect:true,
	    url:'appointment!queryAppointmentsInPage.action',
	    idField: 'id',
	    queryParams :queryParams,
	    columns:[[
	        {field:'appName',title:'仪器名称',width:fixWidth(0.18)},
	        {field:'operatNo',title:'订单号',width:fixWidth(0.17)},
	        {field:'startTime',title:'预约开始时间',width:fixWidth(0.16)},
	        {field:'endTime', title:'预约结束时间',width:fixWidth(0.16)},
	        {field:'checkStatus',title:'状态',width:fixWidth(0.12),align:'center',
	        	    formatter : function(value, rowData, rowIndex) {
	        	    	if (rowData.appType == 0) {
	        	    			if (value == 1) {
									return '待审核';
						 		} 
						 		else if (value == 2) {
									return '课题组长审核通过';
						 		} 
						 		else if (value == 3) {
									return '课题组长审核拒绝';
						 		} 
						 		else if (value == 4) {
									return '管理员审核通过';
						 		} 
						 		else if (value == 5) {
									return '管理员审核拒绝';
						 		}
	        	    	} else if(rowData.appType == 1)  {
	        	    		return '已取消';
	        	    	} 
        	    		return '未知';
					}
			},
			{field:'useType',title:'使用情况',width:fixWidth(0.1),align:'center',
	        	    formatter : function(value, rowData, rowIndex) {
						if (value == 1) {
							return '预约';
				 		} 
				 		else if (value == 2) {
							return '超时使用';
				 		} 
				 		else if (value == 3) {
							return '超时未用';
				 		} 
				 		else if (value == 4) {
							return '已经使用';
				 		} else {
				 			return '未知'
				 		}
					}
			},
			{field:'appType',title:'取消类型',width:fixWidth(0.05),align:'center',hidden:true,
	        	    formatter : function(value, rowData, rowIndex) {
						if (value == 0) {
							return '未取消';
				 		} 
				 		else {
				 			return '已取消'
				 		}
					}
			},
			{field:'id', title:'操作', width:fixWidth(0.1), 
					  formatter : function(value, rowData, rowIndex) {
						if (rowData.useType == 1 && rowData.appType == 0 && rowData.checkStatus == 1) {
							var e = '<a href="#" style="color:red;text-decoration:none;" onclick="cancleOrder('+ rowIndex + ',\'' + value +'\')">取消</a> ';
							return e;
				 		} 
				 		else if (rowData.appType == 1) {
				 			return '已取消'
				 		} else {
				 			return '';
				 		}
					}
			}
	    ]],
	    onBeforeEdit:function(rowIndex,rowData){
			rowData.editing = true;
			$('#appointments').datagrid('refreshRow', rowIndex);
		},
		onAfterEdit:function(rowIndex,rowData){
			rowData.editing = true;
			$('#appointments').datagrid('refreshRow', rowIndex);
		},
	    pagination:true,
	    rownumbers:true,
	    fitColumns:true
	});
}

function cancleOrder(index, value) 
{
	 $.messager.confirm('取消', '您确定要取消该预约?', function(r){
        if (r){
           var params = {
				id : value
			}
			
			var option = {
		   		type: 'POST',
		   		url: 'appointment!cancelAppointments.action',
		   		data: params,
		   		success : function(msg){
		   			if ('success' == msg) {
		   				$.messager.alert('确定', '恭喜您取消成功', 'info')
			     	
		   			} else {
		   				$.messager.alert('确定', '取消失败，请稍后再尝试', 'error')
		   			}
     	     		$('#appointments').datagrid('reload');
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('确定', '处理失败：' + errorThrown, 'error')
				}
			}
			
			$.ajax(option); 
        } else {
        	$('#appointments').datagrid('cancelEdit', index);
        }
     });
}